#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        printf("before: I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        char s1[] = "hello=11";
        putenv(s1);
        printf("子进程环境变量：%s\n",getenv("hello"));
        /* 用execl调用ls */
        // execl("/usr/bin/lsa","ls","-l",NULL);

        /* 用execlp调用ls -a +p会去环境变量搜索进程路径*/
        // execlp("ls","ls","-a",NULL);
        
        /* 用execv调用自定义进程*/
        // char *const argv[] = {"otherExe","-a","-l",NULL};
        // execv("./otherExe",argv);
        // printf("after: I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        // exit(0);

        /* 用execl执行python代码*/
        // execl("/usr/bin/python3","python3","test.py",NULL);
        

        /* 用execle传递环境变量*/
        //自定义环境变量数组
        char *const myenv[] = {
            "MYVAL=11",
            "hello=world",
            "Jaxsen=Jason",
            NULL
        };
        execle("./otherExe","otherExe","-a",NULL,myenv);         
    }
    int status = 0;
    pid_t ret = waitpid(-1,&status, 0);
    printf("父进程环境变量：%s\n",getenv("hello"));
    if(ret > 0)
    {
        printf("wait success, father pid:%d,ret id:%d\n",getpid(),ret);
    }
    return 0;
}